﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using hospital_management_system.ServiceReference1;

namespace hospital_management_system.Controllers
{
    public class HospitalInfoController : Controller
    {
        ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();
        // GET: HospitalInfo
        /// <summary>
        /// 添加医院信息页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.Office = client.T_Office();
            return View();
        }
        /// <summary>
        /// 添加医院信息
        /// </summary>
        /// <returns></returns>
        public ActionResult HospAdd(string Hosp_Name, string Hosp_Message, string Hosp_Phone, string Hosp_Address, FormCollection file1)
        {
            //接收上传按钮的图片
            var file = Request.Files["file1"];
            //接收复选按钮的值
            string Office_ID = Request.Form["Check"];
            if (file.FileName == "")
            {
                return Content("<script>alert('请上传文件')</script>");
            }
            //获取图片的路径
            var filepath = "/HospImg/" + Guid.NewGuid() + file.FileName;
            //转化为虚拟路径
            file.SaveAs(Server.MapPath(filepath));
            ServiceReference1.T_Hospital m = new ServiceReference1.T_Hospital();
            m.Hosp_Address = Hosp_Address;
            m.Hosp_Name = Hosp_Name;
            m.Hosp_Image = filepath;
            m.Hosp_Message = Hosp_Message;
            m.Hosp_Phone = Hosp_Phone;
            m.Office_ID = Office_ID;
            var i = client.T_Hospital_Add(m);
            if (i > 0)
            {
                return Content("<script>alert('添加成功');location.href='/HospitalInfo/HospShow'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败')</script>");
            }
        }
        /// <summary>
        /// 显示全部医院信息页面
        /// </summary>
        /// <returns></returns>
        public ActionResult HospShow()
        {
            ViewBag.Hospital = client.T_Hospital(null);
            return View();
        }
        /// <summary>
        /// 删除医院信息方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult HospDel(int id)
        {
            int i = client.T_Hospital_Del(id);
            if (i > 0)
            {
                return Content("<script>alert('删除成功');location.href='/HospitalInfo/HospShow'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败')</script>");
            }
        }
        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult HospUpt(string id)
        {
            ViewBag.Hospital = client.T_Hospital(id);
            ViewBag.Office = client.T_Office();
            return View();
        }
        /// <summary>
        /// 修改方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult HospUpt(string Hosp_Name, string Hosp_Message, string Hosp_Phone,int Hosp_ID, string Hosp_Address, FormCollection file1)
        {
            //接收上传按钮的图片
            var file = Request.Files["file1"];
            //接收复选按钮的值
            string Office_ID = Request.Form["Check"];
            string filepath = "";
            if (file.FileName != "")
            {
                //获取图片的路径
                filepath += "/HospImg/" + Guid.NewGuid() + file.FileName;
                //转化为虚拟路径
                file.SaveAs(Server.MapPath(filepath));
            }
            ServiceReference1.T_Hospital m = new T_Hospital();
            m.Hosp_Address = Hosp_Address;
            m.Hosp_Name = Hosp_Name;
            m.Hosp_Image = filepath == "" ? m.Hosp_Image : filepath;
            m.Hosp_Message = Hosp_Message;
            m.Hosp_Phone = Hosp_Phone;
            m.Office_ID = Office_ID;
            m.Hosp_ID = Hosp_ID;
            var i = client.T_Hospital_Add(m);
            if (i > 0)
            {
                return Content("<script>alert('修改成功');location.href='/HospitalInfo/HospShow'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败')</script>");
            }
        }
    } 
}